package com.wc.算法提高课.C第三章_图论.有向图的强连通分量.银河;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/21 21:49
 * @description https://www.acwing.com/problem/content/description/370/
 */
public class Main {
    /**
     * 思路：
     *
     * @see com.wc.算法提高课.C第三章_图论.差分约束.糖果
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 600010;
    static int[] h = new int[N], hs = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dfn = new int[N], low = new int[N];
    static int[] stk = new int[N], size = new int[N];
    static int[] dist = new int[N], id = new int[N];
    static boolean[] inStk = new boolean[N];
    static int idx = 1, top = 0, sccCnt = 0, timestamp = 0;
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) add(h, 0, i, 1);
        while (m-- > 0) {
            int t = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
            if (t == 1) {
                add(h, a, b, 0);
                add(h, b, a, 0);
            } else if (t == 2) add(h, a, b, 1);
            else if (t == 3) add(h, b, a, 0);
            else if (t == 4) add(h, b, a, 1);
            else add(h, a, b, 0);
        }
        tarjan(0);
        boolean success = true;
        for (int u = 0; u <= n; u++) {
            for (int i = h[u]; i > 0; i = ne[i]) {
                int j = e[i];
                int a = id[u], b = id[j];
                if (a == b) {
                    if (w[i] > 0) {
                        success = false;
                        break;
                    }
                } else add(hs, a, b, w[i]);
            }
            if (!success) break;
        }
        if (!success) out.println(-1);
        else {
            for (int u = sccCnt; u > 0; u--) {
                for (int i = hs[u]; i > 0; i = ne[i]) {
                    int j = e[i];
                    dist[j] = Math.max(dist[j], dist[u] + w[i]);
                }
            }
            long res = 0;
            for (int i = 1; i <= sccCnt; i++) res += (long) dist[i] * size[i];
            out.println(res);
        }
        out.flush();
    }

    static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk[++top] = u;
        inStk[u] = true;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (dfn[j] == 0) {
                tarjan(j);
                low[u] = Math.min(low[u], low[j]);
            } else if (inStk[j]) {
                low[u] = Math.min(low[u], dfn[j]);
            }
        }
        if (dfn[u] == low[u]) {
            ++sccCnt;
            int y;
            do {
                y = stk[top--];
                inStk[y] = false;
                id[y] = sccCnt;
                size[sccCnt]++;
            } while (y != u);
        }
    }

    static void add(int[] h, int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
